package com.curd.tk.base.service.impl;




import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.curd.common.config.exception.CloudException;
import com.curd.tk.base.dao.IBaseMapper;
import com.curd.tk.base.service.IBaseService;
import com.curd.tk.base.service.impl.tkpackaging.PackagingServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Condition;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * 基于通用MyBatis Mapper插件的Service接口的实现
 */
@Slf4j
public abstract class IBaseAbstractService<DOMAIN, ID, M extends IBaseMapper<DOMAIN>> extends PackagingServiceImpl<DOMAIN, ID, M> implements IBaseService<DOMAIN, ID> {


    @Override
    public String importExcel(Class clazz, MultipartFile file) {
        int count = 0;
        long time = 0;
        try {
            long start = System.currentTimeMillis();
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<DOMAIN> all = reader.readAll(clazz);
            //保存
            count = super.save(all);
            time = System.currentTimeMillis() - start;
        } catch (Exception e) {
            log.error("录入失败" + e);
            throw new CloudException("录入失败");
        }finally {
            try {
                file.getInputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "录入统计: 共成功录入" + count + " 条数据，耗时 " + time + " ms";
    }

    /**
     * ID为当前操作者的ID，即操作者身份
     * 它将与某个值（如登陆后的token，session等中的身份）比对验证
     *
     * @param userCheckId
     * @param msg
     */
    @Override
    public void checkUser(ID userCheckId, String msg) {
        /**
         * 这里只是做了一个测试性的身份验证
         */
        System.out.println("\n预留接口>>>>>>>>>>>>>>>>>>>>>>>>>>>>这里仅供测试验证，具体验证请自行根据需求而定>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>测试验证开始");
        Assert.isTrue("12345".equals(String.valueOf(userCheckId)), "身份效验失败");
        System.out.println("预留接口>>>效验用户权限 操作用户：" + userCheckId + "验证通过-------- 预留接口 效验逻辑请自按需添加");
        System.out.println("预留接口>>>>>>>>>>>>>>>>>>>>>>>>>>>>这里仅供测试验证，具体验证请自行根据需求而定>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>测试验证结束\n");
    }

}